import * as React from 'react';
import { Form, notification } from 'antd';
import IForm from '../../components/IForm';
import validation from '../../utils/validation';
import IButton from '../../components/IButton';

import http from '../../utils/http';


interface State {
    IFormData: any,
    IFormItems: any[],
    id: string
}

class Info extends React.Component<{
    history: any,
    form: any,
}> {

    state: State = {
        IFormItems: [{
            type: "text",
            name: "email",
            label: "账号",
            validation: validation.required,
        }, {
            type: "text",
            name: "mobile",
            label: "手机号",
            validation: validation.required,
        }, {
            type: "string",
            name: "username",
            label: "绑定钱包地址",
            validation: validation.required,
        }, {
            type: "password",
            name: "password",
            label: "修改登录密码",
            validation: validation.password.bind(this),
        }, {
            type: "password",
            name: "passwordAgin",
            label: "确认密码",
            validation: validation.equalPassword.bind(this),
        }],
        IFormData: {},
        id: null,
    }

    componentDidMount() {
        const params = new URLSearchParams(this.props.history.location.search);
        this.state.id = params.get("id");
        this.getData();
    }

    async getData() {
        const { data } = await http.get(`/admin/users/${this.state.id}`);
        this.setState({
            IFormData: data.data,
        })
    }

    async handleSubmit() {
        const value = await validation.validateFields(this.props.form);
        await http.post(`/admin/users/${this.state.id}/update`, value);
        const self = this;
        notification.success({
            message: "提交成功",
            duration: 2,
            onClose() {
                self.props.history.goBack()
            }
        });
    }

    public render() {
        return (
            <div style={{
                maxWidth: 540,
            }}>
                <IForm
                    {...this.props}
                    items={this.state.IFormItems}
                    data={this.state.IFormData}
                />
                <IButton name="确定" type="primary" onClick={this.handleSubmit.bind(this)} />
            </div>
        );
    }



}




export default Form.create({ name: 'Info' })(Info);